package com.project.JunM.controller;

import com.project.JunM.entity.SGather;
import com.project.JunM.entity.SPay;
import com.project.JunM.service.SPayService;
import com.project.common.entity.PageVo;
import com.project.xsf.entity.MProcedure;
import com.project.xsf.service.MProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sPay")
public class SPayController {

    @Autowired
    private SPayService sPayService;
    @Autowired
    private MProcedureService mProcedureService;

    @PostMapping("/addsPay")
    public void addsPay(SPay sPay,String sPayDetailsObj) throws IOException {

        sPayService.insertSelective(sPay,sPayDetailsObj);

    }

    //查询 出库申请审核
    @PostMapping("/queryRegisterCheckAll.action")
    public PageVo<SPay> queryRegisterCheckAll(@RequestBody Map<String,String> map){
        return sPayService.queryRegisterCheckAll(map);
    }

    //审核出库申请单
    @PostMapping("/updateSPayByPayId")
    public String updateSPayByPayId(SPay sPay){

        sPayService.updateSPayByPayId(sPay);
        //返回一个审核标记
        String checkTag = sPay.getCheckTag();

        return checkTag;
    }

    @PostMapping("/queryModuleIfStore.action")
    public Integer queryModuleIfStore(String manufactureId,Integer id,Integer mProcedureDetailsNumber){
        Integer result=0;

        List<SPay> sPays = sPayService.queryAllSPay();
//        System.out.println(sPays);

        for(int i=0;i<sPays.size();i++){

            SPay sPay = sPays.get(i);
//            System.out.println(sPay);
            String reasonexact = sPay.getReasonexact();
            if(reasonexact!=null){
                if(reasonexact.length()!=0&&reasonexact.contains("-")){
                    String[] split = reasonexact.split("-");
                    String mId=split[0];
//                本工序对应的所有物料已调度出库，才能进行工序登记
                    if(mId.equals(manufactureId)&&sPay.getStoreTag().equals("K002-3")){
                        result=1;
                        break;
                    }else{
                        result= -1;
                    }
                }
            }

        }
        //上一个工序未完成时不能对下一个工序进行登记
        boolean b=true;
        List<MProcedure> mProcedures = mProcedureService.queryMProcedureByPId(id);
        for(int i=0;i<mProcedures.size();i++){
            MProcedure mProcedure = mProcedures.get(i);
            if(mProcedure.getDetailsNumber()<mProcedureDetailsNumber){
                //未完成或者未开始
                if(mProcedure.getProcedureFinishTag().equals("G004-0")||mProcedure.getProcedureFinishTag().equals("G004-2")){
                    b=false;
                }
            }
        }
        //前面的工序未完成
        if(!b){
            result=-2;
        }

        return result;
    }
    @PostMapping("/queryModuleIfReCheck.action")
    public int queryModuleIfReCheck(String manufactureId,Integer id,Integer mProcedureDetailsNumber){
        Integer result=0;
        //上一个工序未复核时不能对下一个工序进行登记
        boolean b=true;
        List<MProcedure> mProcedures = mProcedureService.queryMProcedureByPId(id);
        for(int i=0;i<mProcedures.size();i++){
            MProcedure mProcedure = mProcedures.get(i);
            if(mProcedure.getDetailsNumber()<mProcedureDetailsNumber){
                //未审核
                if(!mProcedure.getProcedureTransferTag().equals("G005-2")){
                    b=false;
                }
            }
        }
        //前面的工序未复核
        if(!b){
            result=-2;
        }

        return result;

    }

    //出库申请查询
    @PostMapping("/queryRegisterSelectAll.action")
    public PageVo<SPay> queryRegisterSelectAll(@RequestBody Map<String,String> map){
        return sPayService.queryRegisterSelectAll(map);
    }

    //制定出库单
    @PostMapping("/queryOutSetAll.action")
    public PageVo<SPay> queryOutSetAll(@RequestBody Map<String,String> map){

        List<String> productIdList = sPayService.queryOutSetAll1();

        return sPayService.queryOutSetAll(map,productIdList);
    }

    //出库登记
    @PostMapping("/queryOutCheckInAll.action")
    public PageVo<SPay> queryOutCheckInAll(@RequestBody Map<String,String> map){
        return sPayService.queryOutCheckInAll(map);
    }

    //出库登记审核
    @PostMapping("/queryOutCheckInAuditAll.action")
    public PageVo<SPay> queryOutCheckInAuditAll(@RequestBody Map<String,String> map){
        return sPayService.queryOutCheckInAuditAll(map);
    }

    //出库登记复核成功
    @PostMapping("/updateStoreTagById")
    public void updateStoreTagById(Integer parentId,String checkTime){
        SPay sPay = new SPay();
        sPay.setId(parentId);
        sPay.setStoreTag("K002-3");
        sPay.setCheckTime(checkTime);
        sPayService.updateByPrimaryKeySelective(sPay);
    }

    //出库登记查询
    @PostMapping("/queryOutCheckInSelectAll.action")
    public PageVo<SPay> queryOutCheckInSelectAll(@RequestBody Map<String,String> map){
        return sPayService.queryOutCheckInSelectAll(map);
    }

}
